package com.mzy.concurrent;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.LockSupport;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Author: mizy3
 * @Date: 2024/08/20/下午3:30
 * @Description:
 */
public class 交替打印ABC {

    /*public static final Object lock = new Object();
    public static int state = 0;

    public static void main(String[] args) {
        new Thread(() -> {
            synchronized (lock){
                while (state != 0){
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }

                System.out.println("A");
                state = 1;
                lock.notifyAll();
            }
        }).start();

        new Thread(() -> {
            synchronized (lock){
                while (state != 1){
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                System.out.println("B");
                state = 2;
                lock.notifyAll();
            }
        }).start();

        new Thread(() -> {
            synchronized (lock){
                while (state != 2){
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                System.out.println("C");
                state = 3;
                lock.notifyAll();
            }
        }).start();
    }*/

/*    public static final ReentrantLock lock = new ReentrantLock();
    public static Condition condition = lock.newCondition();
    public static int state = 1;
    public static void main(String[] args) {
        new Thread(() -> {
            while (true){
                lock.lock();
                try {

                    while (state != 1){
                        try {
                            condition.await();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    System.out.println("A");
                    condition.signalAll();
                    state = 2;
                }finally {
                    lock.unlock();
                }
            }
        }).start();

        new Thread(() -> {
            while (true){
                lock.lock();
                try {

                    while (state != 2){
                        try {
                            condition.await();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    System.out.println("B");
                    condition.signalAll();
                    state = 3;
                }finally {
                    lock.unlock();
                }
            }
        }).start();

        new Thread(() -> {
            while (true){
                lock.lock();
                try {

                    while (state != 3){
                        try {
                            condition.await();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    System.out.println("C");
                    state = 1;
                    condition.signalAll();
                }finally {
                    lock.unlock();
                }
            }
        }).start();
    }*/

/*    public static void main(String[] args) {




        Thread t3 = new Thread(() -> {
            LockSupport.park();
            System.out.println("C");
        });
        Thread t2 = new Thread(() -> {
            LockSupport.park();
            System.out.println("B");
            LockSupport.unpark(t3);
        });

        Thread t1 = new Thread(() -> {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("A");
            LockSupport.unpark(t2);
        });
        t1.start();
        t2.start();
        t3.start();
    }*/

    public static void main(String[] args) {
        Thread t3 = new Thread(() -> {
            LockSupport.park();
            System.out.println("C");
        });


        Thread t2 = new Thread(() -> {
            LockSupport.park();
            System.out.println("B");
            LockSupport.unpark(t3);
        });



        Thread t1 = new Thread(() -> {
            System.out.println("A");
            LockSupport.unpark(t2);
        });


        t1.start();
        t2.start();
        t3.start();

    }
}
